package com.summer.Sort;

import java.util.Arrays;

public class day9 {
    public static int binarySearch(int[] arr,int left,int right,int target){
        int mid = (left+right) / 2;
        int midVal = arr[mid];
        if (midVal < target) return binarySearch(arr,mid+1,right,target);
        else if (midVal == target) return mid;
        else return binarySearch(arr,left,mid-1,target);
    }
    public static void quickSort(int[] arr,int left,int right){
        int privot = arr[(left+right) / 2];
        int l = left;
        int r = right;
        int temp = 0;
        while (l < r){
            while (arr[l] < privot) l++;
            while (arr[r] > privot) r--;
            if (l>=r)break;
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            if (arr[l] == privot) r--;
            if (arr[r] == privot) l++;
        }
        if (l==r){
            l++;
            r--;
        }
        if (l < right) quickSort(arr,l,right);
        if (r > left) quickSort(arr,left,r);
    }
    public static void main(String[] args) {
        int N = 8000000;
    int[] arr1 = new int[N];
        for (int i = 0; i < N ; i++) {
        arr1[i] = (int) (Math.random()*N);
    }
    int[] arr = new int[]{2,6,4,8,10,9,15};
    long startTime = System.currentTimeMillis();
    quickSort(arr,0,arr.length-1);
    long endTime =  System.currentTimeMillis();
        System.out.println("排序运行时间:"+(endTime-startTime)+"ms");
    long startTimef = System.currentTimeMillis();
    int index = binarySearch(arr,0,arr.length-1,8);
    long endTimef = System.currentTimeMillis();
        System.out.println("查找运行时间:"+(endTimef-startTimef)+"ms"+"------下标为:"+index);
        System.out.println(Arrays.toString(arr));
}
}
